package cn.buyclss.backend.service.support;

import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author: Cecurio
 * @create: 2019-03-18 17:26
 **/
@Service
public class QiniuService {


    // 设置好账号的ACCESS_KEY和SECRET_KEY
    // @Value("${qiniu.access_key}")
    // TODO 此处@Value无法注入。所以硬编码
    private String accessKey = "CHppCWTVYopwh_7Ik-OW0iGbAWbMSqUTiRxXf_61";

    //@Value("${qiniu.secret_key}")
    private String secretKey = "57vt12dBsb5dCjKswtmoBO-1TLd-jEduaIP3NmKU";

    // 要上传的空间
    //@Value("${qiniu.bucket_name}")
    private String bucketName = "apa-pic";

    // 密钥配置
    private Auth auth = Auth.create(accessKey, secretKey);

    // 创建上传对象
    private UploadManager uploadManager = new UploadManager();

    // 简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public String getUpToken() {
        return auth.uploadToken(bucketName);
    }

    public void upload(byte[] localData, String remoteFileName) throws IOException {
        Response res = uploadManager.put(localData, remoteFileName, getUpToken());
        // 打印返回的信息
        System.out.println(res.bodyString());
    }

}
